home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / WWShaders / WWRustyShinyMetal.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  4.0 KB  |  116 lines

  1. /*
  2.  * WWShinyRustyMetal.sl -- faked environment shiny metal with specks of rust
  3.  * 
  4.  * hacked from Pixar's rsmetal and Larry Gritz's rustymetal shaders
  5.  *
  6.  * DESCRIPTION:
  7.  *   A rough metal surface with controllable rust spots.  The rust pattern
  8.  *   is basically thresholded turbulence (summed abs(snoise)).  Where it's
  9.  *   rusty, shade like rust colored matte, and also make it bumpy (like
  10.  *   the corrosion is kind of grainy).  Where there is no rust, shade like
  11.  *   regular metal.  All computations are done in shader space.
  12.  *
  13.  * PARAMETERS
  14.  *   metalKa, metalKs, metalroughness - control the appearance of the metal.
  15.  *   rustKa, rustKd, rustcolor - control the appearance of the rust.
  16.  *   txtscale - overall scaling factor of the rust pattern.
  17.  *   rusty - 0=no rust, larger for more rust, 1=completely rusty
  18.  *   rustbump - controls the "bumpiness" of the rusty areas.
  19.  *
  20.  * ANTIALIASING:
  21.  *   The fractal sum used to determine the rust pattern chooses a number of
  22.  *   octaves to sum based on the shader sampling rate.  This helps to keep
  23.  *   aliasing under control.
  24.  *
  25.  * AUTHOR: Larry Gritz + Pixar, with minor surgery by wave
  26.  *
  27.  * HISTORY:
  28.  *   27 Feb 59 - wave
  29.  *
  30.  */
  31.  
  32.  
  33.  
  34. /* Signed noise varies from -1 to 1 (like Perlin uses) */
  35. #define snoise(x) (2*noise(x)-1)
  36.  
  37. /* Maximum number of octaves */
  38. #define MAXOCTAVES 8
  39.  
  40.  
  41. surface
  42. WWRustyShinyMetal (float metalKa = 1, metalKs = 1, metalroughness = .1;
  43.         float rustKa = 1, rustKd = 1;
  44.         color rustcolor = color (.437, .084, 0);
  45.         float txtscale = 1;
  46.         float rusty = 0.2;
  47.         float rustbump = 0.035;
  48.         float Kr = 0.6;
  49.     )
  50. {
  51.   point Nf, V;                 /* normal and view vector used for shading */
  52.   point Nrust;                 /* perturbed normal for the rusty areas */
  53.   point PP;                    /* shade space point */
  54.   float i, sum = 0, a = 1;     /* Loop control for fractal sum */
  55.   float alimit;                /* Limit sum to do simple antialiasing */
  56.   float rustiness;             /* Result: how rusty is this point? */
  57.   color Cmetal = 0, Crust = 0; /* Computed colors of metal & rust */
  58.   float w;
  59.   point D;
  60.   color Cr;
  61.  
  62.  
  63.   /* Sum several octaves of abs(snoise), i.e. turbulence.  Limit the
  64.    * number of octaves by the estimated change in PP between adjacent
  65.    * shading samples.
  66.    */
  67.   /* change from "shader" to "object" so that we can use for animation */
  68.   PP = txtscale * transform ("object", P);
  69.   alimit = sqrt (area(PP));
  70.   for (i = 0;  i < MAXOCTAVES  &&  a > alimit;  i += 1) {
  71.       sum += a * abs(snoise(PP));
  72.       PP *= 2;
  73.       a /= 2;
  74.     }
  75.   /* If it's rusty, also add a high frequency bumpiness to the normal */
  76.   Nrust = calculatenormal (P + rustbump * snoise(PP) * normalize(N));
  77.  
  78.   /* Scale the rust appropriately, modulate it by another noise 
  79.    * computation, then sharpen it by squaring its value.
  80.    */
  81.   rustiness = step (1-rusty, clamp (sum,0,1));
  82.   rustiness *= clamp (abs(snoise(PP)), 0, .08) / 0.08;
  83.   rustiness *= rustiness;
  84.  
  85.   /* If we have any rust, calculate the color of the rust, taking into
  86.    * account the perturbed normal and shading like matte.
  87.    */
  88.   if (rustiness > 0) {
  89.       Nf = faceforward (normalize(Nrust),I);
  90.       Crust = rustcolor * (rustKa*ambient() + rustKd*diffuse(Nf));
  91.     }
  92.   /* If we have any metal, calculate the color of the metal, using the
  93.    * original (smooth) normal and the usual metal illumination model.
  94.    */
  95.   /* also add in the fake environment stuff, but only for the shiny part */
  96.   if (rustiness < 1) {
  97.       Nf = faceforward (normalize(N),I);
  98.  
  99.       /* do the fake environment stuff here */
  100.       V = normalize(-I);
  101.       D = reflect(V, Nf) ;
  102.       D = transform("world", D) - transform("world", (0.,0.,0.)) ;
  103.       w = 0.6 * noise(D * 2.5) + 0.2 * noise(D * 5) + 0.1 * noise(D * 10) + 0.05 * noise(D * 20);
  104.       w = w * w * w;
  105.       Cr = Kr * mix(color(0,0,0), color(1,1,1), w );
  106.  
  107.       Cmetal = Cs * (metalKa*ambient() + metalKs*specular(Nf,V,metalroughness) + Cr);
  108.     }
  109.  
  110.   /* Now blend the metal and rust colors depending on the computed value
  111.    * of the rustiness.
  112.    */
  113.   Oi = Os;
  114.   Ci = Oi * mix (Cmetal, Crust, rustiness);
  115. }
  116.